package com.wisdomstar.web.controller.wisdomstar;

import com.wisdomstar.common.core.controller.BaseController;
import com.wisdomstar.common.core.domain.AjaxResult;
import com.wisdomstar.common.exception.base.BaseException;
import com.wisdomstar.ws.domain.Favorites;
import com.wisdomstar.ws.domain.vo.FavoritesVO;
import com.wisdomstar.ws.service.FavoritesService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author RinMi
 * @since 2023/6/6
 */

@RestController
@RequestMapping("ws/favorites")
@Tag(name = "ws-favorites-controller", description = "收藏夹信息管理接口")
public class WsFavoritesController extends BaseController {

    @Resource
    private FavoritesService favoritesService;

    @ApiOperation(value = "分页查询收藏夹信息", notes = "分页查询收藏夹信息", httpMethod = "GET")
    @GetMapping("listFavorites")
    public AjaxResult listFavorites(@ApiParam("父节点") @RequestParam(value = "parentId", defaultValue = "0") Long parentId) {
        try {
            Favorites favorites = new Favorites();
            favorites.setParentId(parentId);
            favorites.setUserId(getUserId());
            List<Favorites> favoritesList = favoritesService.listFavorites(favorites);
            return AjaxResult.success(favoritesList);
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
    }

    @ApiOperation(value = "新增收藏夹信息", notes = "新增收藏夹信息", httpMethod = "POST")
    @PostMapping("insertFavorites")
    public AjaxResult insertFavorites(@ApiParam("收藏夹信息") @RequestBody @Validated FavoritesVO favoritesVO) {
        try {
            Favorites favorites = new Favorites();
            BeanUtils.copyProperties(favoritesVO, favorites);
            favorites.setUserId(getUserId());
            int count = favoritesService.insertFavorites(favorites);
            if (count == 1) {
                return AjaxResult.success("新增成功");
            } else {
                return AjaxResult.error("新增失败");
            }
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    @ApiOperation(value = "修改收藏夹信息", notes = "新增收藏夹信息", httpMethod = "POST")
    @PostMapping("updateFavorites")
    public AjaxResult updateFavorites(@ApiParam("收藏夹信息") @RequestBody FavoritesVO favoritesVO) {
        try {
            Favorites favorites = new Favorites();
            BeanUtils.copyProperties(favoritesVO, favorites);
            favorites.setUserId(getUserId());
            int count = favoritesService.updateFavorites(favorites);
            if (count == 1) {
                return AjaxResult.success("修改成功");
            } else {
                return AjaxResult.error("修改失败");
            }
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    @ApiOperation(value = "删除收藏夹信息", notes = "删除收藏夹信息", httpMethod = "DELETE")
    @DeleteMapping("removeFavorites")
    public AjaxResult removeFavorites(@ApiParam("收藏夹id") @RequestParam Long favoritesId) {
        try {
            Favorites favorites = new Favorites();
            favorites.setFavoritesId(favoritesId);
            favorites.setUserId(getUserId());
            favoritesService.removeFavorites(favorites);
            return AjaxResult.success("删除成功");
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
    }
}
